package com.jiguang.Electricvehicle;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;


import java.text.DecimalFormat;

public class CircleProgressBar extends View {
    private Paint paintCircleBottom = new Paint();
    private Paint paintArcTop = new Paint();
    private Paint paintText = new Paint();

    private int DEFAULT_WIDTH = 100;//默认宽度
    private int DEFAULT_HEIGHT = 100;//默认宽度
    private int DEFAULT_RADIUS = 50;//默认半径

    private float _angle;//弧形的角度

    /***************************** attr *******************************/
    int textColor;
    float textSize;
    int circleColor;
    int arcColor;
    float circleWidth;
    float arcWidth;
    int progress;

    /***************************** attr *******************************/

    public CircleProgressBar(Context context) {
        super(context);
        init();
    }

    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        textColor = typedArray.getColor(R.styleable.CircleProgressBar_textColor, Color.BLACK);
        textSize = typedArray.getDimension(R.styleable.CircleProgressBar_textSize, 14);
        circleColor = typedArray.getColor(R.styleable.CircleProgressBar_circleColor, Color.LTGRAY);
        circleWidth = typedArray.getFloat(R.styleable.CircleProgressBar_circleWidth, 10f);
        arcColor = typedArray.getColor(R.styleable.CircleProgressBar_arcColor, Color.MAGENTA);
        arcWidth = typedArray.getFloat(R.styleable.CircleProgressBar_arcWidth, 10f);
        progress = typedArray.getInt(R.styleable.CircleProgressBar_initProgress, 0);

        typedArray.recycle();
        init();
    }

    public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //初始化文本的画笔
        paintText.setFlags(Paint.ANTI_ALIAS_FLAG);
        paintText.setStyle(Paint.Style.FILL);
        paintText.setColor(textColor);//设置自定义属性值
        paintText.setTextAlign(Paint.Align.CENTER);
        paintText.setTextSize(textSize);
        Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD);
        paintText.setTypeface(typeface);

        //初始化底层圆形的画笔
        paintCircleBottom.setFlags(Paint.ANTI_ALIAS_FLAG);
        paintCircleBottom.setStrokeCap(Paint.Cap.ROUND);
        paintCircleBottom.setStyle(Paint.Style.STROKE);
        paintCircleBottom.setColor(circleColor);//设置自定义属性值
        paintCircleBottom.setStrokeWidth(circleWidth);//设置自定义属性值

     /*   // 创建渐变Shader
        RadialGradient radialGradient = new RadialGradient(
                getWidth() / 2,
                getHeight() / 2,
                getHeight() / 2+,
                Color.RED,
                Color.BLUE,
                Shader.TileMode.CLAMP
        );
        paint.setShader(radialGradient);

        //初始化弧形的画笔
        paintArcTop.setFlags(Paint.ANTI_ALIAS_FLAG);
        paintArcTop.setStrokeCap(Paint.Cap.ROUND);
        paintArcTop.setStyle(Paint.Style.STROKE);
        paintArcTop.setColor(arcColor);//设置自定义属性值
        paintArcTop.setStrokeWidth(arcWidth);//设置自定义属性值*/

        _angle = progress;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(DEFAULT_WIDTH, heightMeasureSpec);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthMeasureSpec, DEFAULT_HEIGHT);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 圆心坐标是(centerX,centerY)
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        //确定半径
        float radius = Math.min(centerX, centerY) - paintCircleBottom.getStrokeWidth();

        // 创建渐变Shader
        RadialGradient radialGradient = new RadialGradient(
                centerX,
                centerY,
                Math.min(getWidth(), getHeight()),
                Color.RED,
                Color.BLUE,
                Shader.TileMode.CLAMP
        );
        Shader shader = new LinearGradient(centerX - radius,
                centerY - radius,
                centerX + radius,
                centerY + radius,
                Color.parseColor("#84C2FD"),
                Color.parseColor("#88B9CBFE"),
                Shader.TileMode.CLAMP
        );


        paintArcTop.setFlags(Paint.ANTI_ALIAS_FLAG);
        paintArcTop.setStrokeCap(Paint.Cap.ROUND);
        paintArcTop.setStyle(Paint.Style.STROKE);
//        paintArcTop.setColor(arcColor);//设置自定义属性值
        paintArcTop.setStrokeWidth(arcWidth);//设置自定义属性值*/
        paintArcTop.setShader(shader);

        //绘制底层圆形
        canvas.drawCircle(centerX, centerY, radius, paintCircleBottom);

        //绘制上层弧形,从顶部开始，顺时针走90°
        canvas.drawArc(centerX - radius, centerY - radius, centerX + radius, centerY + radius, 270, _angle, false, paintArcTop);

        //绘制文本,文字中心和圆心保持一致
//        DecimalFormat dt = new DecimalFormat("0.##");
        DecimalFormat dt = new DecimalFormat("#");//取整
        Paint.FontMetrics fontMetrics = paintText.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
//        float baseline = centerY + distance;
        float baseline = (float) (centerY * 0.78 + distance);
        canvas.drawText(dt.format(100 * _angle / 360) + "", centerX - 90, baseline, paintText);//文字中心和圆心一致
    }

//    public void setProgress(int progress) {
//        this.progress = progress;
//        invalidate();
//    }

    /**
     * 设置进度，展现动画
     */
    public void setProgress(int progress, int targetProgress) {
        ValueAnimator animator = ValueAnimator.ofFloat(progress, targetProgress);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float cur = (float) animation.getAnimatedValue();
                _angle = cur / 100 * 360;
                invalidate();
            }
        });
        animator.setDuration(800);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.start();

    }


    public void animateProgress(int from, int to) {
        ValueAnimator animator = ValueAnimator.ofFloat(from, to);
        animator.setDuration(800);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
//                setProgress((Integer) animation.getAnimatedValue());
                float cur = (float) animation.getAnimatedValue();
                _angle = cur / 100 * 360;
                invalidate();
            }
        });
        animator.start();
    }
}
